package org.jeecg.modules.xdbuse.controller;

import java.io.UnsupportedEncodingException;
import java.io.IOException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.jeecg.common.system.vo.LoginUser;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.xdbuse.entity.FxjBuse0110;
import org.jeecg.modules.xdbuse.entity.FxjBuse0120;
import org.jeecg.modules.xdbuse.entity.FxjBuse0130;
import org.jeecg.modules.xdbuse.entity.FxjBuse0140;
import org.jeecg.modules.xdbuse.entity.FxjBuse0150;
import org.jeecg.modules.xdbuse.entity.FxjBuse0160;
import org.jeecg.modules.xdbuse.entity.FxjBuse01;
import org.jeecg.modules.xdbuse.vo.FxjBuse01Page;
import org.jeecg.modules.xdbuse.service.IFxjBuse01Service;
import org.jeecg.modules.xdbuse.service.IFxjBuse0110Service;
import org.jeecg.modules.xdbuse.service.IFxjBuse0120Service;
import org.jeecg.modules.xdbuse.service.IFxjBuse0130Service;
import org.jeecg.modules.xdbuse.service.IFxjBuse0140Service;
import org.jeecg.modules.xdbuse.service.IFxjBuse0150Service;
import org.jeecg.modules.xdbuse.service.IFxjBuse0160Service;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import com.alibaba.fastjson.JSON;

 /**
 * @Description: 订单管理
 * @Author: zhaodui
 * @Date:   2020-02-20
 * @Version: V1.0
 */
@RestController
@RequestMapping("/xdbuse/fxjBuse01")
@Slf4j
@Api(tags="客户")
public class FxjBuse01Controller {
	@Autowired
	private IFxjBuse01Service fxjBuse01Service;
	@Autowired
	private IFxjBuse0110Service fxjBuse0110Service;
	@Autowired
	private IFxjBuse0120Service fxjBuse0120Service;
	@Autowired
	private IFxjBuse0130Service fxjBuse0130Service;
	@Autowired
	private IFxjBuse0140Service fxjBuse0140Service;
	@Autowired
	private IFxjBuse0150Service fxjBuse0150Service;
	@Autowired
	private IFxjBuse0160Service fxjBuse0160Service;
	
	/**
	 * 分页列表查询
	 *
	 * @param fxjBuse01
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@GetMapping(value = "/list")
	@ApiOperation("客户列表")
	public Result<?> queryPageList(FxjBuse01 fxjBuse01,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<FxjBuse01> queryWrapper = QueryGenerator.initQueryWrapper(fxjBuse01, req.getParameterMap());
		Page<FxjBuse01> page = new Page<FxjBuse01>(pageNo, pageSize);
		IPage<FxjBuse01> pageList = fxjBuse01Service.page(page, queryWrapper);
		return Result.ok(pageList);
	}
	
	/**
	 *   添加
	 *
	 * @param fxjBuse01Page
	 * @return
	 */
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody FxjBuse01Page fxjBuse01Page) {
		FxjBuse01 fxjBuse01 = new FxjBuse01();
		BeanUtils.copyProperties(fxjBuse01Page, fxjBuse01);
		fxjBuse01Service.saveMain(fxjBuse01, fxjBuse01Page.getFxjBuse0110List(),fxjBuse01Page.getFxjBuse0120List(),fxjBuse01Page.getFxjBuse0130List(),fxjBuse01Page.getFxjBuse0140List(),fxjBuse01Page.getFxjBuse0150List(),fxjBuse01Page.getFxjBuse0160List());
		return Result.ok("添加成功！");
	}

	 /**
	  *   添加
	  *
	  * @return
	  */
	 @PostMapping(value = "/addmain")
	 public Result<?> addmain(@RequestBody FxjBuse01 fxjBuse01) {
		 fxjBuse01Service.save(fxjBuse01);
		 return Result.ok(fxjBuse01);
	 }

	 @PostMapping(value = "/addmain10")
	 public Result<?> addmain10(@RequestBody FxjBuse0110 fxjBuse0110) {
		 fxjBuse0110Service.save(fxjBuse0110);
		 return Result.ok(fxjBuse0110);
	 } /**
	  *   添加
	  *
	  * @return
	  */
	 @PostMapping(value = "/addmain20")
	 public Result<?> addmain20(@RequestBody FxjBuse0120 fxjBuse0120) {
		 fxjBuse0120Service.save(fxjBuse0120);
		 return Result.ok(fxjBuse0120);
	 } /**
	  *   添加
	  *
	  * @return
	  */
	 @PostMapping(value = "/addmain30")
	 public Result<?> addmain30(@RequestBody FxjBuse0130 fxjBuse0130) {
		 fxjBuse0130Service.save(fxjBuse0130);
		 return Result.ok(fxjBuse0130);
	 } /**
	  *   添加
	  *
	  * @return
	  */
	 @PostMapping(value = "/addmain40")
	 public Result<?> addmain40(@RequestBody FxjBuse0140 fxjBuse0140) {
		 fxjBuse0140Service.save(fxjBuse0140);
		 return Result.ok(fxjBuse0140);
	 } /**
	  *   添加
	  *
	  * @return
	  */
	 @PostMapping(value = "/addmain50")
	 public Result<?> addmain50(@RequestBody FxjBuse0150 fxjBuse0150) {
		 fxjBuse0150Service.save(fxjBuse0150);
		 return Result.ok(fxjBuse0150);
	 } /**
	  *   添加
	  *
	  * @return
	  */
	 @PostMapping(value = "/addmain60")
	 public Result<?> addmain60(@RequestBody FxjBuse0160 fxjBuse0160) {
		 fxjBuse0160Service.save(fxjBuse0160);
		 return Result.ok(fxjBuse0160);
	 }
	/**
	 *  编辑
	 *
	 * @param fxjBuse01Page
	 * @return
	 */
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody FxjBuse01Page fxjBuse01Page) {
		FxjBuse01 fxjBuse01 = new FxjBuse01();
		BeanUtils.copyProperties(fxjBuse01Page, fxjBuse01);
		FxjBuse01 fxjBuse01Entity = fxjBuse01Service.getById(fxjBuse01.getId());
		if(fxjBuse01Entity==null) {
			return Result.error("未找到对应数据");
		}
		fxjBuse01Service.updateMain(fxjBuse01, fxjBuse01Page.getFxjBuse0110List(),fxjBuse01Page.getFxjBuse0120List(),fxjBuse01Page.getFxjBuse0130List(),fxjBuse01Page.getFxjBuse0140List(),fxjBuse01Page.getFxjBuse0150List(),fxjBuse01Page.getFxjBuse0160List());
		return Result.ok("编辑成功!");
	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		fxjBuse01Service.delMain(id);
		return Result.ok("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.fxjBuse01Service.delBatchMain(Arrays.asList(ids.split(",")));
		return Result.ok("批量删除成功！");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		FxjBuse01 fxjBuse01 = fxjBuse01Service.getById(id);
		if(fxjBuse01==null) {
			return Result.error("未找到对应数据");
		}
		return Result.ok(fxjBuse01);

	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@GetMapping(value = "/queryFxjBuse0110ByMainId")
	public Result<?> queryFxjBuse0110ListByMainId(@RequestParam(name="id",required=true) String id) {
		List<FxjBuse0110> fxjBuse0110List = fxjBuse0110Service.selectByMainId(id);
		return Result.ok(fxjBuse0110List);
	}
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@GetMapping(value = "/queryFxjBuse0120ByMainId")
	public Result<?> queryFxjBuse0120ListByMainId(@RequestParam(name="id",required=true) String id) {
		List<FxjBuse0120> fxjBuse0120List = fxjBuse0120Service.selectByMainId(id);
		return Result.ok(fxjBuse0120List);
	}
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@GetMapping(value = "/queryFxjBuse0130ByMainId")
	public Result<?> queryFxjBuse0130ListByMainId(@RequestParam(name="id",required=true) String id) {
		List<FxjBuse0130> fxjBuse0130List = fxjBuse0130Service.selectByMainId(id);
		return Result.ok(fxjBuse0130List);
	}
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@GetMapping(value = "/queryFxjBuse0140ByMainId")
	public Result<?> queryFxjBuse0140ListByMainId(@RequestParam(name="id",required=true) String id) {
		List<FxjBuse0140> fxjBuse0140List = fxjBuse0140Service.selectByMainId(id);
		return Result.ok(fxjBuse0140List);
	}
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@GetMapping(value = "/queryFxjBuse0150ByMainId")
	public Result<?> queryFxjBuse0150ListByMainId(@RequestParam(name="id",required=true) String id) {
		List<FxjBuse0150> fxjBuse0150List = fxjBuse0150Service.selectByMainId(id);
		return Result.ok(fxjBuse0150List);
	}
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@GetMapping(value = "/queryFxjBuse0160ByMainId")
	public Result<?> queryFxjBuse0160ListByMainId(@RequestParam(name="id",required=true) String id) {
		List<FxjBuse0160> fxjBuse0160List = fxjBuse0160Service.selectByMainId(id);
		return Result.ok(fxjBuse0160List);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param fxjBuse01
    */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, FxjBuse01 fxjBuse01) {
      // Step.1 组装查询条件查询数据
      QueryWrapper<FxjBuse01> queryWrapper = QueryGenerator.initQueryWrapper(fxjBuse01, request.getParameterMap());
      LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();

      //Step.2 获取导出数据
      List<FxjBuse01> queryList = fxjBuse01Service.list(queryWrapper);
      // 过滤选中数据
      String selections = request.getParameter("selections");
      List<FxjBuse01> fxjBuse01List = new ArrayList<FxjBuse01>();
      if(oConvertUtils.isEmpty(selections)) {
          fxjBuse01List = queryList;
      }else {
          List<String> selectionList = Arrays.asList(selections.split(","));
          fxjBuse01List = queryList.stream().filter(item -> selectionList.contains(item.getId())).collect(Collectors.toList());
      }

      // Step.3 组装pageList
      List<FxjBuse01Page> pageList = new ArrayList<FxjBuse01Page>();
      for (FxjBuse01 main : fxjBuse01List) {
          FxjBuse01Page vo = new FxjBuse01Page();
          BeanUtils.copyProperties(main, vo);
          List<FxjBuse0110> fxjBuse0110List = fxjBuse0110Service.selectByMainId(main.getId());
          vo.setFxjBuse0110List(fxjBuse0110List);
          List<FxjBuse0120> fxjBuse0120List = fxjBuse0120Service.selectByMainId(main.getId());
          vo.setFxjBuse0120List(fxjBuse0120List);
          List<FxjBuse0130> fxjBuse0130List = fxjBuse0130Service.selectByMainId(main.getId());
          vo.setFxjBuse0130List(fxjBuse0130List);
          List<FxjBuse0140> fxjBuse0140List = fxjBuse0140Service.selectByMainId(main.getId());
          vo.setFxjBuse0140List(fxjBuse0140List);
          List<FxjBuse0150> fxjBuse0150List = fxjBuse0150Service.selectByMainId(main.getId());
          vo.setFxjBuse0150List(fxjBuse0150List);
          List<FxjBuse0160> fxjBuse0160List = fxjBuse0160Service.selectByMainId(main.getId());
          vo.setFxjBuse0160List(fxjBuse0160List);
          pageList.add(vo);
      }

      // Step.4 AutoPoi 导出Excel
      ModelAndView mv = new ModelAndView(new JeecgEntityExcelView());
      mv.addObject(NormalExcelConstants.FILE_NAME, "订单管理列表");
      mv.addObject(NormalExcelConstants.CLASS, FxjBuse01Page.class);
      mv.addObject(NormalExcelConstants.PARAMS, new ExportParams("订单管理数据", "导出人:"+sysUser.getRealname(), "订单管理"));
      mv.addObject(NormalExcelConstants.DATA_LIST, pageList);
      return mv;
    }

    /**
    * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
      MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
      Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();
      for (Map.Entry<String, MultipartFile> entity : fileMap.entrySet()) {
          MultipartFile file = entity.getValue();// 获取上传文件对象
          ImportParams params = new ImportParams();
          params.setTitleRows(2);
          params.setHeadRows(1);
          params.setNeedSave(true);
          try {
              List<FxjBuse01Page> list = ExcelImportUtil.importExcel(file.getInputStream(), FxjBuse01Page.class, params);
              for (FxjBuse01Page page : list) {
                  FxjBuse01 po = new FxjBuse01();
                  BeanUtils.copyProperties(page, po);
                  fxjBuse01Service.saveMain(po, page.getFxjBuse0110List(),page.getFxjBuse0120List(),page.getFxjBuse0130List(),page.getFxjBuse0140List(),page.getFxjBuse0150List(),page.getFxjBuse0160List());
              }
              return Result.ok("文件导入成功！数据行数:" + list.size());
          } catch (Exception e) {
              log.error(e.getMessage(),e);
              return Result.error("文件导入失败:"+e.getMessage());
          } finally {
              try {
                  file.getInputStream().close();
              } catch (IOException e) {
                  e.printStackTrace();
              }
          }
      }
      return Result.ok("文件导入失败！");
    }

}
